#include<iostream>

using namespace std;

int gcd(int a, int b){
	return b?gcd(b,a%b):a;
}

int main(){
	int p, q, result;
	cin>>p>>q;
	int g = gcd(p, q);
    p /= g; p %= 2;
    q /= g; q %= 2;

	p == 1 ? result=0 : result=2;
    if (p == 1 && q == 1) result = 1;
    
    cout<<result;

    return 0;
} 

